package com.ebupt.migu.music.approve.controller;

import com.ebupt.migu.music.approve.service.SoundService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author xuhongsong
 * @version 1.0
 * @date 2020/10/28 16:18
 */
@RestController
@RequestMapping("/sound")
public class SoundController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SoundService soundService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 获取听荐实时状态
     *
     * @return
     */
    @PostMapping("/reco")
    public Map<String,Object> reco() {
        String uid=request.getParameter("uid");
        Map<String,Object>  map=new HashMap<>();
        if (StringUtils.isNotBlank(uid)){
            map.put("uid",uid);
            Object recoDefault=redisTemplate.opsForValue().get("recoDefault");
            Object recoDisplay=redisTemplate.opsForValue().get("recoDisplay");
            if (recoDefault==null){
                Map<String,Object> resultMap=this.soundService.soundReco();
                recoDefault=Optional.ofNullable(recoDefault).orElse(Optional.ofNullable(resultMap).orElse(new HashMap<>()).get("recoDefault"));
                recoDisplay=Optional.ofNullable(recoDisplay).orElse(Optional.ofNullable(resultMap).orElse(new HashMap<>()).get("recoDisplay"));
                redisTemplate.opsForValue().set("recoDefault", recoDefault.toString());
                redisTemplate.opsForValue().set("recoDisplay", recoDisplay.toString());
            }
            map.put("recoDefault",recoDefault);
            map.put("recoDisplay",recoDisplay);
        }
        return map;
    }

    /**
     *  修改
     * @param
     * @param
     * @return
     */
    @PostMapping("/update")
    public ResultObject update() {
        ResultObject resultObject=new ResultObject();
        Integer recoDefault=Integer.parseInt(request.getParameter("recoDefault"));
        Integer recoDisplay=Integer.parseInt(request.getParameter("recoDisplay"));
        if (recoDefault!=null && recoDisplay!=null){
            redisTemplate.opsForValue().set("recoDefault", recoDefault.toString());
            redisTemplate.opsForValue().set("recoDisplay", recoDisplay.toString());
            this.soundService.update(recoDefault,recoDisplay);
            resultObject.setMsg("操作成功！");
            resultObject.setCode("0000");
            resultObject.setData(null);
            return resultObject;
        }
        resultObject.setMsg("数据不合法！");
        resultObject.setCode("1111");
        resultObject.setData(null);
        return resultObject;
    }


    /**
     *  查询版本号
     * @return
     */
    @GetMapping("/app_version")
    public ResultObject select() {
        List<Map<String,Object>> mapList= soundService.selectAppVersion();
        ResultObject resultObject=new ResultObject();
        resultObject.setMsg("查询成功！");
        resultObject.setCode("0000");
        resultObject.setData(mapList);
        return resultObject;
    }
}
